package imitate;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:TQ02
 * Data:2024-02-01
 * Time:19:48
 * Text：N字形变换
 */
public class Text2 {
    public static String convert(String s, int numRows) {
        char[] array=s.toCharArray();
        char[] arr=new char[array.length];
        int n=array.length;
        int d=2*numRows-2;
        int num=0;
        for(int k=0;k< array.length;k+=d){
            arr[num++]=array[k];
        }
        for(int i=1;i<numRows-1;i++){
            for(int k=i,j=d-i;k<n || j<n;k+=d,j+=d){
                arr[num++]=array[k];
                if(j<=n-1){
                    arr[num++]=array[j];
                }
            }
        }
        for(int k=numRows-1;k< array.length;k+=d){
            arr[num++]=array[k];
        }
        return new String(arr);
    }

    public static void main(String[] args) {
        String s="PAYPALISHIRING";
        int i=3;
        System.out.println(convert(s,i));

    }
}
